import json
from django.core import serializers
from django.shortcuts import render

# Create your views here.

# 查询课程分类
from rest_framework.response import Response
from rest_framework.views import APIView

from course.models import CourseModel, NperModel, ShufflingModel


# 首页获取课程
class GetQueryCourseView(APIView):
    def get(self, request):
        c = CourseModel.objects.filter()
        if len(c) == 0:
            data = {
                'msg': "没有查询到",
                'code': 4000,
            }
            return Response(data)
        else:
            data_list = []
            c1 = json.loads(serializers.serialize("json", c))
            for i in c1:
                print("ii",i)
                data_dict = {}
                data_dict['id'] = i["pk"]
                data_dict['name'] = i["fields"]["name"]
                data_dict['money'] = i["fields"]["money"]
                data_dict['introduce'] = i["fields"]["introduce"]
                data_dict['image'] = i["fields"]["image"]
                n = NperModel.objects.filter(id=i["fields"]["nper"])
                n1 = json.loads(serializers.serialize("json", n))[0]['fields']
                data_dict['number'] = n1["number"]
                data_dict['apply'] = n1["apply"]
                data_dict['residue'] = n1["residue"]
                data_dict['apply_start_time'] = n1["apply_start_time"]
                data_dict['apply_end_time'] = n1["apply_end_time"]
                data_dict['start_time'] = n1["start_time"]
                data_dict['end_time'] = n1["end_time"]
                data_dict['learning'] = n1["learning"]
                data_list.append(data_dict)

            data = {
                'msg': "没有查询到",
                'code': 4000,
                'data': data_list
            }
            return Response(data)


# 查看课程详情
class DetailsView(APIView):
    def post(self, request, *args, **kwargs):
        try:
            a = json.loads(request.body)
            id = a['id']
            # id = request.POST['id']

            # id = json.loads(request.body)
        except:

            data = {
                'msg': "课程id有误",
                'code': 4000,
            }
            return Response(data)
        else:
            c = CourseModel.objects.filter(id=id)
            c1 = json.loads(serializers.serialize("json", c))[0]
            data_dict = {}
            data_dict['id'] = c1["pk"]
            data_dict['name'] = c1["fields"]["name"]
            data_dict['money'] = c1["fields"]["money"]
            data_dict['introduce'] = c1["fields"]["introduce"]
            data_dict['image'] = c1["fields"]["image"]
            n = NperModel.objects.filter(id=c1["fields"]["nper"])
            n1 = json.loads(serializers.serialize("json", n))[0]['fields']
            data_dict['number'] = n1["number"]
            data_dict['apply'] = n1["apply"]
            data_dict['residue'] = n1["residue"]
            data_dict['apply_start_time'] = n1["apply_start_time"]
            data_dict['apply_end_time'] = n1["apply_end_time"]
            data_dict['start_time'] = n1["start_time"]
            data_dict['end_time'] = n1["end_time"]
            data_dict['learning'] = n1["learning"]
            data = {
                'msg': "查询成功",
                'code': 2000,
                'data': data_dict
            }
            return Response(data)


# 轮播图
class BanerView(APIView):
    def get(self, request):
        s = ShufflingModel.objects.filter(status=0)
        s1 = json.loads(serializers.serialize("json", s))
        data_list = []
        for i in s1:
            data_dict = {}
            data_dict['image'] = "http://192.168.3.113:8000/"+i['fields']['image']
            data_list.append(data_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': data_list
        }
        return Response(data)
